package com.zry.controller.collect;

import com.zry.VO.CollectVo;
import com.zry.entity.Collect;
import com.zry.service.collect.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Random;

@Controller
public class CollectController {

    @Autowired
    @Qualifier("CollectServiceImpl")
//    @Qualifier("CollectServiceImplByStreamImpl")
    private CollectService collectService;

    @GetMapping({"/", "/index"})
    public ModelAndView index(String cid) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/collect/index");
        return modelAndView;
    }

    @GetMapping("/api/collect/load")
    @ResponseBody
    public List<CollectVo> getCollectList() {
        return collectService.findCollectByCategoryId();
    }

    @GetMapping("/api/collect/loadMore")
    @ResponseBody
    public List<Collect> getCollectList(Integer categoryId
            , @RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "8") Integer pageSize) {

        return collectService.findCollectByPageNo(categoryId,pageNo,pageSize);
    }


    @PostMapping("/api/collect/delete/{id}")
    @ResponseBody
    public boolean deleteCollect(@PathVariable("id") long id){
        return collectService.removeById(id);
    }

    @PostMapping("/api/collect/saveOrUpdate")
    @ResponseBody
    public Collect deleteCollect(@RequestBody Collect collect){
        boolean flag = collectService.saveOrUpdate(collect);
        return flag?collect:null;
    }

}
